#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>

#define NUM 120
#define CMD_NUM 40

int main()
{
  char command[NUM];
  char* argv[CMD_NUM];
  while(1)
  {
    command[0] = 0;
    printf("[shy@VM-8-3-centos lesson11]# ");
    fflush(stdout);
    fgets(command, NUM , stdin);
    command[strlen(command)-1] = 0;
    //  printf("echo: %s",command);
    // 解析命令
    int i = 1;
    char* delim = " ";
    argv[0] = strtok(command , delim);
    while(argv[i] = strtok(NULL , delim))
    {
      i++;
    }

    // 内建命令
    if (strcmp(argv[0], "cd") == 0)
       {
         if (argv[1] != NULL)
         {
           chdir(argv[1]);
         }

         continue; // 下面不执行了
       }

    if (fork() == 0)
    {
      // child 
      execvp(argv[0] , argv);
      exit(1);
    }

    waitpid(-1 , NULL , 0);
  }
  return 0;
}
